﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;

using Kwan.Data;
using BG.Data;

namespace BG.Service
{
    public class CollectionQuery
	{

		public static Expression MakeSearchWhere(NameValueCollection ps)
		{
			Expression exp = new Expression();

            string propName = "EntityID";
            if (!string.IsNullOrEmpty(ps[propName]))
                exp &= TCollection.entityID == int.Parse(ps[propName]);

            propName = "billNO";
            if (!string.IsNullOrEmpty(ps[propName]))
            {
                exp &= TCollection.billNO == ps[propName];
            }
            propName = "cashPay";
            if (!string.IsNullOrEmpty(ps[propName]))
            {
                exp &= TCollection.cashPay == double.Parse(ps[propName]);
            }
            propName = "change";
            if (!string.IsNullOrEmpty(ps[propName]))
            {
                exp &= TCollection.change == double.Parse(ps[propName]);
            }
            propName = "createdDate";
            if (!string.IsNullOrEmpty(ps[propName]))
            {
                exp &= TCollection.createdDate == DateTime.Parse(ps[propName]);
            }
            propName = "customerID";
            if (!string.IsNullOrEmpty(ps[propName]))
            {
                exp &= TCollection.customerID == int.Parse(ps[propName]);
            }
            propName = "invoice";
            if (!string.IsNullOrEmpty(ps[propName]))
            {
                exp &= TCollection.invoice == double.Parse(ps[propName]);
            }
            propName = "invoiceNO";
            if (!string.IsNullOrEmpty(ps[propName]))
            {
                exp &= TCollection.invoiceNO == ps[propName];
            }
            propName = "memo";
            if (!string.IsNullOrEmpty(ps[propName]))
            {
                exp &= TCollection.memo == ps[propName];
            }
            propName = "orderID";
            if (!string.IsNullOrEmpty(ps[propName]))
            {
                exp &= TCollection.orderID == int.Parse(ps[propName]);
            }
            propName = "paid";
            if (!string.IsNullOrEmpty(ps[propName]))
            {
                exp &= TCollection.paid == double.Parse(ps[propName]);
            }
            propName = "pid";
            if (!string.IsNullOrEmpty(ps[propName]))
            {
                exp &= TCollection.pid == int.Parse(ps[propName]);
            }
            propName = "posPay";
            if (!string.IsNullOrEmpty(ps[propName]))
            {
                exp &= TCollection.posPay == double.Parse(ps[propName]);
            }
            propName = "productID";
            if (!string.IsNullOrEmpty(ps[propName]))
            {
                exp &= TCollection.productID == int.Parse(ps[propName]);
            }
            propName = "receivable";
            if (!string.IsNullOrEmpty(ps[propName]))
            {
                exp &= TCollection.receivable == double.Parse(ps[propName]);
            }
            propName = "received";
            if (!string.IsNullOrEmpty(ps[propName]))
            {
                exp &= TCollection.received == double.Parse(ps[propName]);
            }
            propName = "roomID";
            if (!string.IsNullOrEmpty(ps[propName]))
            {
                exp &= TCollection.roomID == int.Parse(ps[propName]);
            }
            propName = "status";
            if (!string.IsNullOrEmpty(ps[propName]))
            {
                exp &= TCollection.status == int.Parse(ps[propName]);
            }
            propName = "userID";
            if (!string.IsNullOrEmpty(ps[propName]))
            {
                exp &= TCollection.userID == ps[propName];
            }

            propName = "tag";
            if (!string.IsNullOrEmpty(ps[propName]))
            {
                string val = ps[propName];
                string[] vals = val.Split(',');
                if (!string.IsNullOrEmpty(vals[0]))
                {
                    exp &= VCollection.roomNO == vals[0];
                }
                bool startNull=string.IsNullOrEmpty(vals[1]);
                bool endNull=string.IsNullOrEmpty(vals[2]);
                if(!startNull && !endNull)
                {
                    DateTime dt1 = DateTime.Parse(vals[1] + " 6:00:00");
                    DateTime dt2 = DateTime.Parse(vals[2] + " 6:00:00").AddDays(1);
                    exp &= VCollection.createdDate.Between(dt1, dt2);
                }
                else if (startNull && !endNull)
                {
                    exp &= VCollection.createdDate <= DateTime.Parse(vals[2] + " 6:00:00").AddDays(1);
                }
                else if (!startNull && endNull)
                {
                    exp &= VCollection.createdDate >= DateTime.Parse(vals[1] + " 6:00:00");
                }
            }

			return exp;
		}
	}
}